package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.modual.entity.GoodsSpec;
import com.micro.modual.entity.GoodsTemplate;
import com.micro.modual.extension.GoodsSpecExtension;
import com.micro.modual.mapper.GoodsSpecMapper;
import com.micro.modual.mapper.GoodsTemplateMapper;
import com.micro.modual.model.bo.goodsspec.GoodsSpecBO;
import com.micro.modual.model.bo.goodsspec.GoodsSpecExistBO;
import com.micro.modual.service.GoodsSpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
*  <h1>商品规格表 属性规格  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsSpecService")
public class GoodsSpecServiceImpl extends AbstractBasicsServiceImpl<GoodsSpec,Long> implements GoodsSpecService {

    @Autowired
    private GoodsSpecMapper goodsSpecMapper;

    @Autowired
    private GoodsTemplateMapper goodsTemplateMapper;
    
    /**
     * <h2>新增商品规格表 属性规格详情</h2>
     * @param goodsSpecBO {@link GoodsSpecBO} 商品规格表 属性规格 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addGoodsSpec(GoodsSpecBO goodsSpecBO) {
        GoodsSpec goodsSpec = GoodsSpecExtension.converAddGoodsSpec(goodsSpecBO);
        goodsSpecMapper.insertSelective(goodsSpec);
        GoodsTemplate goodsTemplate = goodsTemplateMapper.findTemplateByCode(goodsSpecBO.getTemplateCode());
        goodsTemplate.setSpecCount(goodsTemplate.getSpecCount() + 1);
        goodsTemplateMapper.updateByPrimaryKeySelective(goodsTemplate);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改商品规格表 属性规格详情</h2>
     * @param goodsSpecBO  {@link GoodsSpecBO} 商品规格表 属性规格 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editGoodsSpec(GoodsSpecBO goodsSpecBO) {
        GoodsSpec goodsSpec = GoodsSpecExtension.converEditGoodsSpec(goodsSpecBO);
        goodsSpecMapper.updateByPrimaryKeySelective(goodsSpec);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除商品规格表 属性规格信息</h2>
     * @param id  商品规格表 属性规格 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteGoodsSpec(Long id) {
        GoodsSpec goodsSpec = goodsSpecMapper.selectByPrimaryKey(id);
        GoodsTemplate goodsTemplate = goodsTemplateMapper.findTemplateByCode(goodsSpec.getTemplateCode());
        goodsTemplate.setSpecCount(goodsTemplate.getSpecCount() -1);
        goodsTemplateMapper.updateByPrimaryKeySelective(goodsTemplate);
        goodsSpecMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>﻿校验规格名是否存在</h2>
     * @param goodsSpecExistBO {@link GoodsSpecExistBO}
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson validataSpecNameExist(GoodsSpecExistBO goodsSpecExistBO) {
        if(goodsSpecMapper.findSpecExist(goodsSpecExistBO.getSpecName(),goodsSpecExistBO.getTemplateCode()) > 0 ){
            throw new BizException(MicroResponseCode.GOODS_SPEC_HAS_EXIST.getResponseCode(), MicroResponseCode.GOODS_SPEC_HAS_EXIST.getResponseMessage());
        }else{
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

}
